package com.open.capacity.paper.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.model.Paper;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.paper.service.PaperService;
import com.open.capacity.paper.vo.PaperRecordInfoVo;
import com.open.capacity.paper.vo.PaperVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liangd
 * @since 2021-02-01 14:35
 */
@Api(tags = "问卷管理")
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    /**
     * 模糊查询
     */
    @GetMapping("/getPapers")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true,dataType = "Integer")
    })
    @ApiOperation(value = "后台管理查询所有的问卷标识")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    public PageResult<PaperVo> getPapers(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return paperService.getPapers(params);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 新增或更新
     */
    @PostMapping("/saveOrUpdate")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "后台管理新增或者更新问卷标识,papeid==null?新增:更新")
    public Result saveOrUpdate(@RequestBody Paper paper) throws ControllerException {
        try {
            if (paper.getPapeid() != null) {
                return paperService.updatePaper(paper);
            } else {
                return paperService.savePaper(paper);
            }
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据id查询问卷
     */
    @GetMapping("/findById/{papeid}")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "根据id查询问卷")
    public Result findById(@PathVariable Integer papeid) throws ControllerException{
        try {
            return paperService.findById(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("/delete/{papeid}")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "后台管理删除问卷标识")
    public Result delete(@PathVariable Integer papeid) throws ControllerException {
        try {
            return paperService.deletePaper(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据问卷id查问卷内容(问题以及选项)
     */
    @GetMapping("/getQuestionAndOption/{papeid}")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "后台管理根据问卷id查问卷内容(问题以及选项)")
    public Result getQuestionAndOption(@PathVariable Integer papeid) throws ControllerException{
        try {
            return paperService.getQuestionAndOption(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 保存问卷数据
     */
    @PostMapping("/savePaperData")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "后台管理保存问卷数据")
    public Result savePaperData(@RequestBody PaperRecordInfoVo paperRecordInfoVo) throws ControllerException{
        try {
            return paperService.savePaperData(paperRecordInfoVo);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 修改问卷数据
     */
    @PostMapping("/updatePaperData")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "修改问卷数据")
    public Result updatePaperData(@RequestBody PaperRecordInfoVo paperRecordInfoVo) throws ControllerException{
        try {
            return paperService.updatePaperData(paperRecordInfoVo);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据用户Id以及问卷Id查问卷记录
     */
    @GetMapping("/findPaperRecord")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "根据用户Id以及问卷Id查问卷记录数据")
    public Result findPaperRecord(@RequestParam Map<String, Object> params)throws ControllerException{
        try {
            return paperService.findPaperRecord(params);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据用户Id以及问卷Id查问卷记录详情
     */
    @GetMapping("/findPaperInfo")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "根据用户Id以及问卷Id查问卷记录详情")
    public Result findPaperInfo(@RequestParam Integer papeid,@RequestParam Long userid)throws ControllerException{
        try {
            return paperService.findPaperInfo(papeid,userid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取评分统计数据(饼状图)
     */
    @GetMapping("/getPaperPieData/{papeid}")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "获取评分统计数据(饼状图)")
    public Result getPaperPieData(@PathVariable Integer papeid) throws ControllerException{
        try {
            return paperService.getPaperPieData(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取评分统计数据(条形图)
     */
    @GetMapping("/getPaperLinearData/{papeid}")
    @LogAnnotation(module = "paper-center",recordRequestParam=true)
    @ApiOperation(value = "获取评分统计数据(条形图)")
    public Result getPaperLinearData(@PathVariable Integer papeid) throws ControllerException{
        try {
            return paperService.getPaperLinearData(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }
}
